// ignore_for_file: constant_identifier_names

import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';

const DEFAULT_TOAST_DURATION = Duration(seconds: 3);
const DEFAULT_TOAST_COLOR = Color(0xBA424242);

class ToastUtils {
  ToastUtils._internal();

  ///全局初始化Toast配置, child为MaterialApp
  static init(Widget child) {
    return OKToast(
      ///字体大小
      textStyle: const TextStyle(fontSize: 16, color: Colors.white),
      backgroundColor: DEFAULT_TOAST_COLOR,
      radius: 10,
      dismissOtherOnShow: true,
      textPadding: const EdgeInsets.fromLTRB(20, 10, 20, 10),
      duration: DEFAULT_TOAST_DURATION,
      child: child,
    );
  }

  static void toast(String msg, {Duration duration = DEFAULT_TOAST_DURATION, Color color = DEFAULT_TOAST_COLOR}) {
    showToast(
      msg,
      duration: duration,
      backgroundColor: DEFAULT_TOAST_COLOR,
      textStyle: const TextStyle(
        color: Colors.white,
      ),
    );
    // showToastWidget(
    //   _buildSimpleFrame(
    //     msg,
    //     color,
    //   ),
    //   duration: duration,
    // );
  }

  static ToastFuture warning(String msg,
      {Duration duration = DEFAULT_TOAST_DURATION, Color color = DEFAULT_TOAST_COLOR}) {
    //showToast(msg, duration: duration, backgroundColor: Colors.yellow);
    return showToastWidget(
      buildFrame(
        msg,
        color,
        _typeWarning,
      ),
      duration: duration,
    );
  }

  static ToastFuture error(String msg,
      {Duration duration = DEFAULT_TOAST_DURATION, Color color = DEFAULT_TOAST_COLOR}) {
    //showToast(msg, duration: duration, backgroundColor: Colors.red);
    return showToastWidget(
      buildFrame(
        msg,
        color,
        _typeError,
      ),
      duration: duration,
    );
  }

  static ToastFuture success(String msg,
      {Duration duration = DEFAULT_TOAST_DURATION, Color color = DEFAULT_TOAST_COLOR}) {
    //showToast(msg, duration: duration, backgroundColor: Colors.lightGreen);
    return showToastWidget(
      buildFrame(
        msg,
        color,
        _typeSuccess,
      ),
      duration: duration,
    );
  }
}

const int _typeWarning = 101;
const int _typeError = 102;
const int _typeSuccess = 103;

Widget buildFrame(String msg, Color color, int whichType) {
  IconData? iconData = Icons.access_alarm;
  switch (whichType) {
    case _typeSuccess:
      iconData = Icons.done;
      break;
    case _typeWarning:
      iconData = Icons.warning;
      break;
    case _typeError:
      iconData = Icons.error;
      break;
  }
  return buildBaseFrame(
    msg: msg,
    color: color,
    size: 70,
    icon: Icon(
      iconData,
      color: Colors.white,
      size: 70,
    ),
  );
}

Widget buildBaseFrame({
  required String msg,
  Color? color,
  required double size,
  required Widget icon,
}) {
  return Column(
    mainAxisSize: MainAxisSize.min,
    children: [
      Container(
        width: 150,
        padding: const EdgeInsets.only(left: 12, right: 12, top: 12, bottom: 12),
        decoration: BoxDecoration(
          color: color ?? DEFAULT_TOAST_COLOR,
          borderRadius: BorderRadius.circular(8),
        ),
        child: Center(
          child: Column(mainAxisSize: MainAxisSize.min, children: [
            SizedBox(
              height: size,
              width: size,
              child: icon,
            ),
            const SizedBox(
              height: 7,
            ),
            Text(
              msg,
              style: const TextStyle(fontSize: 16, color: Colors.white),
              textAlign: TextAlign.center,
            ),
          ]),
        ),
      ),
    ],
  );
}

// Widget _buildSimpleFrame(String msg, Color color) {
//   var borderSide = BorderSide(width: 1, color: color);
//   return Container(
//     height: 80,
//     width: 140,
//     padding: const EdgeInsets.all(8),
//     decoration: BoxDecoration(
//       color: Colors.white,
//       borderRadius: BorderRadius.circular(8),
//       border: Border(
//         bottom: borderSide,
//         top: borderSide,
//         left: borderSide,
//         right: borderSide,
//       ),
//     ),
//     child: Center(
//       child: Column(mainAxisSize: MainAxisSize.min, children: [
//         Text(
//           msg,
//           style: TextStyle(fontSize: 16, color: color),
//         ),
//       ]),
//     ),
//   );
// }
